//
//  HomeViewController.m
//  BaseProject
//
//  Created by 王洋 on 2021/2/3.
//  Copyright © 2021 王洋. All rights reserved.
//

#import "HomeViewController.h"
#import "SessionListModel.h"
#import "SessionListCell.h"

@interface HomeViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) SessionListModel *model;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
        if (isLogin) {
            [self request_SessionsList];
        }
    }];

}


#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.model.sessions.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SessionListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SessionListCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;

    [cell fillWithModel:self.model.sessions[indexPath.row]];
    return cell;
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"SessionListCell" bundle:nil] forCellReuseIdentifier:@"SessionListCellID"];
    }
    return _tableview;
}

- (SessionListModel *)model {
    if (!_model) {
        _model = [[SessionListModel alloc] init];
        _model.morning = @[].mutableCopy;
        _model.afternoon = @[].mutableCopy;
        _model.night = @[].mutableCopy;
        _model.sessions = @[].mutableCopy;
    }
    return _model;
}

- (void)request_SessionsList {

    NSMutableDictionary *dic = @{}.mutableCopy;
    
    NSString *url = [NSString stringWithFormat:@"%@%@",BA_Global_Domain,API_SessionsList];
    
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                
                
                
            } else {
                
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
                
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [SVProgressHUD showErrorWithStatus:@"请求异常"];
        [SVProgressHUD dismissWithDelay:1];
    }];
}


@end
